const {Op} = require("sequelize");
const { where } = require("sequelize/dist");
const Book = require("../model/book");
const sequelize =require("sequelize");
const { contains } = require("sequelize/dist/lib/operators");
//获取单个书籍
exports.findBook = async function(id){
    const result = await Book.findByPk(id);
    return result;
  };

//修改书籍余数
exports.updataSum = async function(bookId,num=-1){
    const book = await exports.findBook(bookId);
  
   if(book.sum > 0){
     const result = await book.increment({sum:+num});
    
    return result;
   }else{
       return null;
   }
    
}
//添加书籍
exports.addBook= async function(data){
  let result =null;
  if(data instanceof Array){
     result = await Book.bulkCreate(data);
  }else if(data instanceof Object){
       if(data.bookName && data.imgUrl && data.author && data.ISBN && data.price && data.pageNumber && data.type && data.sum &&data.dreUp){
          data.sum = +data.sum;
          result = await Book.create(data);
       } 
  }
  return result;
}
//获取书籍分类
exports.getBookType= async function(){

  const result = await Book.findAll({
    attributes:["type"],
    group:["type"]

  })
  return result;

}

//模糊查找
exports.findBookLike = async function(search){

    let result = await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        author:{
                [Op.like]:`%${search}%`
              }
         
      },
      offset:0,
      limit:10
    })  

    result = result.concat(await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        bookName:{
          [Op.like]:`%${search}%`
        }
      },
      offset:0,
      limit:10
    })  )
    result = result.concat(await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        oBookName:{
          [Op.like]:`%${search}%`
        }
      },
      offset:0,
      limit:2
    })  )
    result = result.concat(await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        tarnslator:{
          [Op.like]:`%${search}%`
        }
      },
      offset:0,
      limit:2
    })  )
    result = result.concat(await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        press:{
          [Op.like]:`%${search}%`
        }
      },
      offset:0,
      limit:2
    })  )
    result = result.concat(await Book.findAll({
      attributes:{exclude:["updatedAt","deletedAt"]},
      where:{
        ISBN:{
          [Op.like]:`%${search}%`
        }
      },
      offset:0,
      limit:2
    })  )
  
    return  {
      rows :result,
      count : result.length
    } 
  }

//根据条件查找书籍
exports.findBooks = async function(obj){
  const where={}
  const type = obj.type || -1;
  const limit = obj.limit || 8; 
  const page = obj.page || 1;
   if(obj.bookName){
     where.bookName= {
      [Op.like] :`%${obj.bookName}%`
     
    };
   }
   if(obj.oBookName){
     where.oBookName = obj.oBookName;
   }
   if(obj.author){
     where.author = {
      [Op.like] : `%${obj.author}%`
     
    };
   }
   if(type !==-1){
     where.type = type;
   }
   if(obj.tarnslator){
     where.tarnslator = {
       [Op.like] : `%${obj.tarnslator}%`
      
     };
   }
   if(obj.ISBN){
     where.ISBN =obj.ISBN;
   }
   const result = await Book.findAndCountAll({
     attributes:{exclude:["updatedAt","deletedAt"]},
     where,
     order:sequelize.col("ISBN"),
     offset: (page-1) * limit,
     limit: +limit,
   });

   return result;

}
exports.getBooks = async function(page = 1,limit = 10){

    const result =  await Book.findAndCountAll({
      offset:(page - 1) * limit,
      limit
    });

    return result;
}
exports.deleBook = async function(id){

  const result = await Book.destroy({
    where:{
      id
    }
  });

  return result;
}
exports.updataBook = async function(id,data){
  if(data){
    const result = await Book.update(obj,{
      where:{
        id
      }
    })

    return result;
 }else{
   return null;
 }
  
}  